public class StateMachine {

    // 定义状态常量
    private static final int STATE_IDLE = 0;
    private static final int STATE_RUNNING = 1;
    private static final int STATE_PAUSED = 2;

    // 当前状态
    private int currentState;

    // 构造函数，设置初始状态
    public StateMachine() {
        this.currentState = STATE_IDLE;
    }

    // 状态转换方法
    public void changeState(int newState) {
        System.out.println("更改状态由 " + currentState + " 到 " + newState);
        this.currentState = newState;
    }

    // 执行当前状态的行为
    public void performAction() {
        switch (currentState) {
            case STATE_IDLE:
                System.out.println("机器空闲.");
                break;
            case STATE_RUNNING:
                System.out.println("机器运行.");
                break;
            case STATE_PAUSED:
                System.out.println("机器暂停.");
                break;
            default:
                System.out.println("未知状态.");
                break;
        }
    }

    // 主函数
    public static void main(String[] args) {
        StateMachine sm = new StateMachine();

        // 初始状态
        sm.performAction();

        // 改变状态
        sm.changeState(STATE_RUNNING);
        sm.performAction();

        // 改变状态
        sm.changeState(STATE_PAUSED);
        sm.performAction();

        // 改变状态
        sm.changeState(STATE_IDLE);
        sm.performAction();
    }
}
